package algorithm;
//迪杰斯特拉算法求最短路径问题
//计算一个结点到其他结点的最短路径
//他的主要特点是以起点为中心，向外层层扩展（bfs），直到扩展到终点为止
import java.util.Arrays;
public class Dijkstra {
    public static void main(String[] args) {
        char[] vertex = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
        final int N = 65535;
        int[][] matrix = {
                {N, 5, 7, N, N, N, 2},
                {5, N, N, 9, N, N, 3},
                {7, N, N, N, 8, N, N},
                {N, 9, N, N, N, 4, N},
                {N, N, 8, N, N, 5, 4},
                {N, N, N, 4, 5, N, 6},
                {2, 3, N, N, 4, 6, N}
        };
        Graph graph = new Graph(vertex, matrix);
//        graph.show();
        graph.dsj(6);
        graph.showDjs();
    }

}


class Graph {
    private char[] vertex;
    private int[][] matrix;
    private VisitedVertex vv;//已经访问顶点的集合

    public Graph(char[] vertex, int[][] matrix) {
        this.vertex = vertex;
        this.matrix = matrix;
    }

    public void showDjs() {
        vv.show();
    }

    public void show() {
        for (int[] link : matrix) {
            System.out.println(Arrays.toString(link));
        }
    }

    public void dsj(int index) {
        vv = new VisitedVertex(vertex.length, index);
        update(index);
        for (int j = 1; j < vertex.length; j++) {
            index = vv.updateAll();
            update(index);
        }
    }


    //更新index下标到周围顶点的距离和走位顶点的前驱结点
    public void update(int index) {
        int len = 0;
        for (int j = 0; j < matrix[index].length; j++) {
            len = vv.getDis(index) + matrix[index][j];
            if (!vv.in(j) && len < vv.getDis(j)) {
                vv.updatePre(j, index);
                vv.updateDis(j, len);
            }
        }
    }
}

class VisitedVertex {
    public int[] already_arr;
    public int[] pre_visited;
    public int[] dis;

    public VisitedVertex(int length, int index) {
        this.already_arr = new int[length];
        this.pre_visited = new int[length];
        this.dis = new int[length];
        Arrays.fill(dis, 65535);
        this.dis[index] = 0;
        this.already_arr[index] = 1;
    }

    //判断index顶点是否被访问过，如果访问过，则返回true
    public boolean in(int index) {
        return already_arr[index] == 1;
    }

    public void updateDis(int index, int len) {
        dis[index] = len;
    }

    public void updatePre(int pre, int index) {
        pre_visited[pre] = index;
    }

    public int getDis(int index) {
        return dis[index];
    }

    //继续选择并返回新的访问结点。
    public int updateAll() {
        int min = 65535, index = 0;
        for (int i = 0; i < already_arr.length; i++) {
            if (already_arr[i] == 0 && dis[i] < min) {
                min = dis[i];
                index = i;
            }
        }
        already_arr[index] = 1;
        return index;
    }

    public void show() {
        System.out.println("============");
        System.out.println(Arrays.toString(already_arr));
        System.out.println("============");
        System.out.println(Arrays.toString(pre_visited));
        System.out.println("============");
        System.out.println(Arrays.toString(dis));
    }
}